package com.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;


import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.*;

/**
 * @description: hbaseAPI
 * @projectName: hadoopstudy
 * @see: com.hbase
 * @author: 崔宗祺
 * @createTime: 2024/5/15 9:10
 * @version: 1.0
 */
public class HbaseStudy {
    @Test
    public void createConn() throws IOException {
        //同步链接
        Configuration configuration = HBaseConfiguration.create();
        //指定zookeeper地址
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        //构建链接
        Connection connection = ConnectionFactory.createConnection(configuration);
        System.out.println("connection = " + connection);
//        connection.getTable()
    }

    @Test
    public void createAsyncConn() throws ExecutionException, InterruptedException, TimeoutException {
        //异步构件链接 异步链接会单独启动线程构建链接对象 不会去等待程序的其他内容
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        CompletableFuture<AsyncConnection> asyncConnection = ConnectionFactory.createAsyncConnection(configuration);
        System.out.println("asyncConnection = " + asyncConnection);
        AsyncConnection asyncConnection1 = asyncConnection.get(1000000, TimeUnit.MILLISECONDS);
        System.out.println("asyncConnection1 = " + asyncConnection1);
    }

    @Test
    public void createNameSpace() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        //构建命名空间制造者
        NamespaceDescriptor.Builder xyy = NamespaceDescriptor.create("xyy");
        NamespaceDescriptor bui = xyy.build();
        //同步创建命名空间
        admin.createNamespace(bui);
    }

    @Test
    public void createNameSpaceAsync() throws IOException, ExecutionException, InterruptedException, TimeoutException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        NamespaceDescriptor.Builder htl = NamespaceDescriptor.create("htl");
        NamespaceDescriptor build = htl.build();
        Future<Void> namespaceAsync = admin.createNamespaceAsync(build);
        namespaceAsync.get(100000, TimeUnit.MILLISECONDS);

    }

    @Test
    public void listNameSpaceData() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        NamespaceDescriptor[] namespaceDescriptors = admin.listNamespaceDescriptors();
        System.out.println(Arrays.toString(namespaceDescriptors));
    }

    @Test
    public void listTableData() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        TableName[] tableNames = admin.listTableNames();
        System.out.println(Arrays.toString(tableNames));
    }

    @Test
    public void deleteNameSpace() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        admin.deleteNamespace("htl");
    }

    @Test
    public void createTableData() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        Admin admin = connection.getAdmin();
        TableName tableName = TableName.valueOf("briup:dog1");
        TableDescriptorBuilder tableDescriptorBuilder = TableDescriptorBuilder.newBuilder(tableName);
        ColumnFamilyDescriptorBuilder cfdb = ColumnFamilyDescriptorBuilder.newBuilder("fl".getBytes());
        ColumnFamilyDescriptor cfd = cfdb.build();
        cfdb.setMaxVersions(5);
        cfdb.setTimeToLive(10000);
        tableDescriptorBuilder.setColumnFamily(cfd);
        TableDescriptor build = tableDescriptorBuilder.build();
        admin.createTable(build);
    }
    @Test
    public void getTableData() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "192.168.10.11:2181,192.168.10.12:2181");
        Connection connection = ConnectionFactory.createConnection(configuration);
        TableName tableName = TableName.valueOf("briup:cat");
        Table table = connection.getTable(tableName);
        Get get = new Get(Bytes.toBytes("100000"));
        Result result = table.get(get);
    }

}
